package test;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
//
// Here are the dbcp-specific classes.
// Note that they are only used in the setupDataSource
// method. In normal use, your classes interact
// only with the standard JDBC API
//
import org.apache.commons.dbcp.BasicDataSource;
//
// Here's a simple example of how to use the BasicDataSource.
// In this example, we'll construct the BasicDataSource manually,
// but you could also configure it using an external conifguration file.
//
//
// Note that this example is very similiar to the PoolingDriver
// example.
//
// To compile this example, you'll want:
// * commons-pool.jar
// * commons-dbcp.jar
// * j2ee.jar (for the javax.sql classes)
// in your classpath.
//
// To run this example, you'll want:
// * commons-collections.jar
// * commons-pool.jar
// * commons-dbcp.jar
// * j2ee.jar (for the javax.sql classes)
// * the classes for your (underlying) JDBC driver
// in your classpath.
//
// Invoke the class using two arguments:
// * the connect string for your underlying JDBC driver
// * the query you'd like to execute
// You'll also want to ensure your underlying JDBC driver
// is registered. You can use the "jdbc.drivers"
// property to do this.
//
// For example:
// java -Djdbc.drivers=oracle.jdbc.driver.OracleDriver \
// -classpath commons-collections.jar:commons-pool.jar:commons-dbcp.jar:j2ee.jar:oracle-jdbc.jar:. \
// BasicDataSourceExample
// "jdbc:oracle:thin:scott/tiger@myhost:1521:mysid"
// "SELECT * FROM DUAL"
//
//===
//In this case we use McKoi database server:
//Embedded server:
//"jdbc:mckoi:local://resource/nz.co.transparent.client.db.conf" "select * from Client"
//Multi-user server:
//"jdbc:mckoi://localhost" "select * from Client"
//===
public class BasicDataSourceExample2 {
public static void main(String[] args) {
// Set url and sql
//String connectionURL = "jdbc:mckoi:local://resource/nz.co.transparent.client.db.conf";
String connectionURL = "jdbc:mckoi://localhost";
String sql = "select * from Client";
// //
// // First we load the underlying JDBC driver.
// // You need this if you don't use the jdbc.drivers
// // system property.
// //
// System.out.println("Loading underlying JDBC driver.");
// // Register the Mckoi JDBC Driver
// try {
// Class.forName("com.mckoi.JDBCDriver").newInstance();
// } catch (Exception e) {
// System.out.println(
// "Unable to register the JDBC Driver.\n"
// + "Make sure the JDBC driver is in the\n"
// + "classpath.\n");
// System.exit(1);
// }
// System.out.println("JDBC driver instantiated.");
// First we set up the BasicDataSource.
// Normally this would be handled auto-magically by
// an external configuration, but in this example we'll
// do it manually.
//
System.out.println("Setting up data source.");
DataSource dataSource = setupDataSource(connectionURL);
System.out.println("Done.");
//
// Now, we can use JDBC DataSource as we normally would.
//
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try {
System.out.println("Creating connection.");
conn = dataSource.getConnection();
System.out.println("Creating statement.");
stmt = conn.createStatement();
System.out.println("Executing statement.");
rset = stmt.executeQuery(sql);
System.out.println("Results:");
int numcols = rset.getMetaData().getColumnCount();
while(rset.next()) {
for(int i=1;i<=numcols;i++) {
System.out.print("\t" + rset.getString(i));
}
System.out.println("");
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
try { rset.close(); } catch(Exception e) { }
try { stmt.close(); } catch(Exception e) { }
try { conn.close(); } catch(Exception e) { }
}
}
public static DataSource setupDataSource(String connectURI) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mckoi.JDBCDriver");
ds.setUsername("admin_user");
ds.setPassword("client00");
ds.setUrl(connectURI);
return ds;
}
public static void printDataSourceStats(DataSource ds) {
BasicDataSource bds = (BasicDataSource) ds;
System.out.println("NumActive: " + bds.getNumActive());
System.out.println("NumIdle: " + bds.getNumIdle());
}
public static void shutdownDataSource(DataSource ds) throws SQLException {
BasicDataSource bds = (BasicDataSource) ds;
bds.close();
}
}